import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Tools, Bubble } from 'weaver-mobile';
import { ListViewPage, RouteLayout,Share } from 'weaver-mobile-page';
import * as adapter from '../../constants/adapter';
const getLabel = Tools.getLabel;
export default class ChanceCardStore {
  comObj = {};
  params = {};
  @observable tabs = [
    { title: getLabel('2121', '详细信息'), key: 'info' },
    { title: getLabel('17532', '联系记录'), key: 'contact' }
  ];
  @observable fieldItems = [];
  @observable contacterItems = [];
  @observable contacterCount = 0;
  @observable chanceId = "";
  @observable customerId = "";
  @observable chanceName = "";
  @observable chanceManager = "";
  @observable chanceOthers = "";
  @observable creater = "";
  @observable chanceConcerned = false;
  @observable canEdit = false;
  @observable initResponsed = false;// 是否已经得到初始化数据的响应
  @observable initSuccessed = false;// 初始化数据是否获取成功
  @observable initFailedMsg = "";   // 初始化处理失败的原因
  @observable tabPage = "info";
  @observable refreshing = false;
  // list
  @observable contactListStore = {};
  // layout
  @observable createContacterLayout = {};
  @observable contacterListLayout = {};
  @observable contacterCardLayout = {};
  @observable customerCardLayout = {};
  @observable chanceViewAndEditLayout = {};
  @observable shareListLayout = {};
  @observable isPro = false;

  @observable canShare = false;
  @observable shareSellChanceLayout = {};
  @observable shareStore = {};
  @observable userId = "";
  @observable onlyView =false;


  @action
  doNew = (comObj, params) => {
    this.comObj = comObj;
    this.params = params;
    if (params.tabPage) {
      this.tabPage = params.tabPage;
    }
    window.crmChance = this;
  }

  @action
  doInit = () => {
    let params = this.params;
    this.refreshing = true;
    Apis.getChanceCard(params).then(data => {
      this.doInitCallBack(params, data);
    });
  }

  @action
  doInitCallBack = (params, data) => {
    this.refreshing = false;
    this.initResponsed = true;
    if (data.status == "0") {
      this.initSuccessed = false;
      this.initFailedMsg = data.errorMsg;
    } else {
      const currentPath = this.comObj.props.location.pathname;
      this.contactListStore = new ListViewPage.Store({ route: currentPath, id: "contactListStore" });
      this.createContacterLayout = new RouteLayout.Store({ route: currentPath, id: 'createContacterLayout' });
      this.contacterListLayout = new RouteLayout.Store({ route: currentPath, id: 'contacterListLayout' });
      this.contacterCardLayout = new RouteLayout.Store({ route: currentPath, id: 'contacterCardLayout' });
      this.customerCardLayout = new RouteLayout.Store({ route: currentPath, id: 'customerCardLayout' });
      this.shareListLayout = new RouteLayout.Store({ route: currentPath, id: 'shareListLayout' });
      this.chanceViewAndEditLayout = new RouteLayout.Store({ route: currentPath, id: 'chanceViewAndEditLayout' });

      this.shareSellChanceLayout = new RouteLayout.Store({ route: currentPath, id: 'shareSellChanceLayout' });
      this.shareStore = new Share.Store({ route: currentPath+'/shareSellChanceLayout', id: 'share' });
   



      this.initSuccessed = true;
      this.isPro = data.isPro;
      this.canEdit = data.canEdit;

      this.canShare = data.canShare;
      this.userId  = data.loginid;//分享要用
      this.onlyView = data.onlyView;

      this.fieldItems = data.chanceInfo;
      this.contacterItems = data.contacterInfo;
      this.contacterCount = data.contacterCount;
      this.chanceId = data.chanceId;
      this.customerId = data.customerId;
      this.chanceName = data.chanceName;
      this.chanceManager = data.chanceManager;
      this.chanceOthers = data.chanceOthers;
      this.chanceConcerned = data.chanceConcerned;
      this.creater=data.creater;
    }
  }

  @action
  doClear = () => {
    this.initResponsed = false;
    this.initSuccessed = false;
    this.initFailedMsg = "";
    this.tabPage = "info";
  }

  @action
  setChanceConcerned(concerned) {
    this.chanceConcerned = !concerned;
    let params = { relatedIds: this.chanceId, type: "sellchance", important: concerned ? 0 : 1 };
    Apis.setChanceConcerned(params).then(data => {
      Bubble.success(getLabel('83551', "保存成功"), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2);
      if (window.crmGlobal && window.crmGlobal.listTabIndex == "attention") {
        window.crmGlobal.doSearch();
      }
    });
  }


  @action
  tabChange(page) {
    this.tabPage = page;
    if (page == "contact") {
      this.getContactList();
    }
  }

  @action
  getContactList() {
    const params = { chanceid: this.chanceId };
    Apis.getContactListResult(params).then(data => {
      const dataKey = data.datas;
      this.contactListStore.clearData();
      this.contactListStore.getData({ dataKey });
    });
  }

  //删除商机
  @action
  doDelete = () => {
    const params = { sellChanceIds: this.chanceId };
    Apis.chanceDelete(params).then(data => {
      if (data.status === "success") {
        Bubble.success(getLabel('83472', '删除成功!'), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2,
          // this.doInit()
          () => {
            this.comObj.props.history.go(-1);
            if (typeof (this.comObj.props.cardStore !== "undefined") && this.comObj.props.cardStore) {
              if (this.comObj.props.cardStore.tabChange("chance")) {
                this.comObj.props.cardStore.tabChange("chance");
              } else {
                this.comObj.props.cardStore.doInit();
              }
            }
          }
        );
        window.crmGlobal && window.crmGlobal.doSearch();
      }
    });
  }
}